<?php
/**
 * Created by PhpStorm.
 * User: jieai
 * Date: 2017-04-17
 * Time: 20:52
 */
namespace Home\Controller;

use Think\Controller;
class LoginController extends MyController{
    public function Login(){
	    session('islogin', 1);
//        die;
        $usernum = M('admin')->find();
        if (!$usernum) {
            $admin = M('admin');
            $admin->username = 'admin';
            $admin->userpwd = 'admin';
            $admin->add();
        }


            if (session('islogin')){
            $this->redirect('home/index/index');
        }else{
            $username=I('username','');
            $userpwd=I('userpwd','');
            $con['username']=$username;
            $con['userpwd']=$userpwd;
            $result=M('admin')->where($con)->find();
            if ($result) {
                session('islogin',1);
                $this->success('登陆成功!','/gs_php');
            }else{
                $this->error('登陆失败!');
            }
        }



        
        
    }
    public function index(){

        if (session('islogin')){
            $this->redirect('home/index/index');
        }else{
            $this->display();
        }

    }

    public function loginout()
    {
        session('islogin',null);
        $this->redirect('home/index/index');
    }

    public function changepwd()
    {
        $this->Checklogin();
        $newpwd = I('pwd');

        if ($newpwd) {
            $admin = M('admin');
            $data['username'] = 'admin';
            $result = $admin->where($data)->setField('userpwd', $newpwd);
            if ($result) {
                $this->success('修改成功,请重新登录');
            }
        }
        $this->error('修改失败');

    }
    
}